#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <unordered_map>

using namespace std;

// 2953. 统计完全子字符串
// https://leetcode.cn/problems/count-complete-substrings/description/

class Solution
{
public:
    // int statisticNum(string str, int k){
    //     int n = str.size();
    //     int ret = 0;
    //     for (int cnt = 1; cnt <= 26; cnt++)
    //     {
    //         int left = 0;
    //         unordered_map<char, int> countOfCh;
    //         int diff = 0;
    //         for (int i = 0; i < n; i++)
    //         {
    //             char curCh = str[i];
    //             countOfCh[curCh]++;
               

    //             if(countOfCh[curCh] > k || countOfCh.size() > cnt){
    //                 diff++;
    //             }

    //             if(i - left + 1 < cnt * k){
    //                 continue;
    //             }

    //             if(diff == 0){
    //                 ret++;
    //             }
    //             --countOfCh[str[left]];
    //             if(countOfCh.size() >= cnt ){
    //                 diff--;
    //             }
    //             if(countOfCh[str[left]] == 0){
    //                 countOfCh.erase(str[left]);
    //             }
    //             left++;
    //         }
    //     }
    //     return ret;
    // }

    int statisticNum(string str, int k) {
        int n = str.size();
        int ret = 0;

        for (int cnt = 1; cnt <= 26; cnt++) {
            int len = cnt * k;
            if (n < len)
                continue;

            unordered_map<char, int> freq;
            int goodChar = 0;
            int badChar = 0;

            for (int i = 0; i < n; i++) {
                char ch = str[i];
                freq[ch]++;
                if (freq[ch] == k)
                    goodChar++;
                if (freq[ch] >= k + 1)
                    badChar++;

                if (i >= len) {
                    char leftChar = str[i - len];
                    if (freq[leftChar] == k)
                        goodChar--;
                    if (freq[leftChar] >= k + 1)
                        badChar--;
                    freq[leftChar]--;
                    if (freq[leftChar] == 0)
                        freq.erase(leftChar);
                }

                if (i >= len - 1 && goodChar == cnt && badChar == 0 &&
                    freq.size() == cnt) {
                    ret++;
                }
            }
        }

        return ret;
    }
    int countCompleteSubstrings(string word, int k)
    {
        int i = 0;
        int n = word.size();
        int ans = 0;
        while (i < n)
        {
            int left = i;
            i++;
            for (; i < n && abs(word[i] - word[i - 1]) <= 2;i++)
            {
                
            }
            string curStr(word.begin() + left, word.begin() + i);
            ans += statisticNum(curStr, k);
        }
        return ans;
    }
};

int main()
{
    string str = "igigee";
    int k = 2;
    Solution solutin{};
    solutin.countCompleteSubstrings(str, k);
    return 0;
}